/**
 * JAVACC DEMO 1.0
 * @Package com.apache.uct.service.plugins
 */
package com.apache.dict.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.dict.entity.DataCate;
import com.apache.dict.entity.DataItem;
import com.apache.dict.manager.DataCateManager;
import com.apache.dict.manager.DataItemManager;
import com.apache.tools.ClassToolsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DictSavePluginImpl implements PluginConnector {

	private Logger log = LoggerFactory.getLogger(DictSavePluginImpl.class);

	private DataItemManager dataItemManager;

	private DataCateManager dataCateManager;

	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();
		try {
			result.setResult("true");
			String doCode = String.valueOf(vo.getParams("doCode"));
			String cateId = String.valueOf(vo.getParams("cateId"));
			String itemId = String.valueOf(vo.getParams("itemId"));
			if ("dictCateSave".equals(doCode) && Validator.isNull(cateId)) {
				//1、判断是否存在
				String cateEname = String.valueOf(vo.getParams("cateEname"));
				ParamsVo<DataCate> dataCateVo = new ParamsVo<DataCate>();
				dataCateVo.setMethodKey("checkCateEname");
				dataCateVo.setParams("cateEname", cateEname);
				boolean counts = (Boolean)dataCateManager.execute(dataCateVo);
				if (!counts) {
					log.info("dictCateSave时发现，科目标识["+cateEname+"]存在相同记录数："+counts);
					result.setResult("false");
					result.setMessage("执行失败：标识["+cateEname+"]不能重复！");
					return result;
				}
				//2、保存数据
				addDictCate(result, vo);
			} else if ("dictItemSave".equals(doCode) && Validator.isNull(itemId)) {
				addDictItem(result, vo);
			} else if ("dictCateSave".equals(doCode) && Validator.isNotNull(cateId)) {
				editDictCate(result, vo);
			} else if ("dictItemSave".equals(doCode) && Validator.isNotNull(itemId)) {
				editDictItem(result, vo);
			} else {
				result.setMessage("没有可用方法被执行！");
			}
		} catch (Exception e) {
			result.setResult("false");
			result.setMessage("执行失败！");
		}
		return result;
	}

	private void addDictCate(ResultEntity result, ParamsVo vo) {
		DataCate dataCate = new DataCate();
		dataCate = (DataCate) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), dataCate);

		if (dataCate != null) {
			ParamsVo<DataCate> dataCateVo = new ParamsVo<DataCate>();
			dataCateVo.setObj(dataCate);
			String cateId = dataCateManager.saveInfo(dataCateVo);
			if (Validator.isNull(cateId)) {
				result.setMessage("新增失败");
			} else {
				result.setEntity(cateId);
				result.setMessage("新增成功");
			}
		}
	}
	
	private void addDictItem(ResultEntity result, ParamsVo vo) {
		DataItem dataItem = new DataItem();
		dataItem = (DataItem) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), dataItem);

		if (dataItem != null) {
			ParamsVo<DataItem> dataCateVo = new ParamsVo<DataItem>();
			dataCateVo.setObj(dataItem);
			dataCateVo.setObj(dataItem);
			String itemId = dataItemManager.saveInfo(dataCateVo);
			if (Validator.isNull(itemId)) {
				result.setMessage("新增失败");
			} else {
				result.setEntity(itemId);
				result.setMessage("新增成功");
			}
		}
	}

	private void editDictCate(ResultEntity result, ParamsVo vo) {
		DataCate dataCate = new DataCate();
		dataCate = (DataCate) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), dataCate);
		if (dataCate != null) {
			ParamsVo<DataCate> param = new ParamsVo<DataCate>();
			if (Validator.isNotNull(dataCate.getCateId())) {
				param.setObj(dataCate);
				boolean flag = dataCateManager.editInfo(param);
				if (!flag) {
					result.setResult("false");
					result.setMessage("修改失败");
				} else {
					result.setResult("true");
					result.setMessage("修改成功");
					result.setEntity(dataCate.getCateId());
				}
			} else {
				result.setMessage("唯一标识未传递或传递的唯一标识不存在");

			}
		}
	}

	private void editDictItem(ResultEntity result, ParamsVo vo) {
		DataItem dataItem = new DataItem();
		dataItem = (DataItem) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), dataItem);
		if (dataItem != null) {
			ParamsVo<DataItem> param = new ParamsVo<DataItem>();
			if (Validator.isNotNull(dataItem.getItemId())) {
				param.setObj(dataItem);
				boolean flag = dataItemManager.editInfo(param);
				if (!flag) {
					result.setResult("false");
					result.setMessage("修改失败");
				} else {
					result.setResult("true");
					result.setMessage("修改成功");
					result.setEntity(dataItem.getItemId());
				}
			} else {
				result.setMessage("唯一标识未传递或传递的唯一标识不存在");

			}
		}
	}

	private String getParamStr(ParamsVo vo, String attrName) {
		return Validator.getDefaultStr(String.valueOf(vo.getParams(attrName)), "");
	}

	public void setDataItemManager(DataItemManager dataItemManager) {
		this.dataItemManager = dataItemManager;
	}

	public void setDataCateManager(DataCateManager dataCateManager) {
		this.dataCateManager = dataCateManager;
	}

}
